package com.backend.system.controller;

import com.backend.system.beans.permission.GrantPermissionToRoleReq;
import com.backend.system.beans.permission.ListPermissionByRoleReq;
import com.backend.system.beans.permission.ListPermissionByRoleVO;
import com.backend.system.beans.permission.PermissionAddOrUpdateReq;
import com.backend.system.common.PageBean;
import com.backend.system.common.ResponseEntity;
import com.backend.system.service.IPermissionService;
import com.backend.system.service.manage.PermissionManage;
import com.backend.system.util.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author backend-manage
 * @since 2024-04-02
 */
@Api(tags = "权限表 前端控制器")
@RestController
@RequestMapping("/sys/permission")
public class PermissionController {

    @Resource
    private IPermissionService permissionService;

    /**
     * 新增或修改权限
     */
    @ApiOperation("新增或修改权限")
    @PostMapping("/addOrUpdate")
    public ResponseEntity<Boolean> addOrUpdate(@Valid @RequestBody PermissionAddOrUpdateReq req) {
        return ResponseUtil.success(PermissionManage.addOrUpdate(req));
    }

    /**
     * 按角色查询全部权限（标识已拥有、未拥有）
     */
    @ApiOperation("按角色查询全部权限（标识已拥有、未拥有）")
    @PostMapping("/listPermissionByRole")
    public ResponseEntity<PageBean<ListPermissionByRoleVO>> listPermissionByRole(@Valid @RequestBody ListPermissionByRoleReq req) {
        req.setDefaultPageInfo();
        return ResponseUtil.success(permissionService.listPermissionByRole(req));
    }

    /**
     * 给角色分配权限
     */
    @ApiOperation("给角色分配权限")
    @PostMapping("/grantPermissions")
    public ResponseEntity<Boolean> grantPermissions(@Valid @RequestBody GrantPermissionToRoleReq req) {
        return ResponseUtil.success(PermissionManage.grantPermissions(req));
    }
}
